﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Models;
using InternshipManagement.Helpers;

namespace InternshipManagement.Controllers
{
    public class DocumentController : Controller
    {
        DocumentRepository documentRepository = new DocumentRepository();
        //
        // GET: /Document/
        [CustomAuthorize(Privileges = UserPrivileges.DisplayDocuments)]
        public ActionResult Index()
        {

            IQueryable<Document> documents = null;

            documents = documentRepository.FindAllDocuments();

            return View(documents);
        }
        [CustomAuthorize(Privileges = UserPrivileges.ListDocuments)]
        public ActionResult List(int? page)
        {
            const int pageSize = 10;

            IQueryable<Document> documents = null;

            documents = documentRepository.FindAllDocuments();

            var paginatedDocuments = new PaginatedList<Document>(documents, page ?? 0, pageSize);

            return View(paginatedDocuments);
        }

        [CustomAuthorize(Privileges = UserPrivileges.AddDocument)]
        public ActionResult Create()
        {
            ViewBag.Op = "Create";
            return View("CreateEdit");
        }

        [HttpPost]
        [CustomAuthorize(Privileges = UserPrivileges.AddDocument)]
        public ActionResult Create(Document document, HttpPostedFileBase attachment)
        {
            if (ModelState.IsValid)
            {
                string path = documentRepository.uploadDocument(attachment);
                if (path != null)
                {
                    document.Path = path;
                    document.UpdateDate = DateTime.Now;
                    documentRepository.AddDocument(document);
                    documentRepository.Save();
                    return RedirectToAction("List");
                }
                else
                {
                    ModelState.AddModelError("Path", "A file must be selected");
                }
            }
            ViewBag.Op = "Create";
            return View("CreateEdit", document);
        }

        [CustomAuthorize(Privileges = UserPrivileges.EditDocument)]
        public ActionResult Edit(int id)
        {
            Document document = documentRepository.GetDocument(id);
            if (document == null)
            {
                return View("ShowMessage", "Document does not exists");
            }
            ViewBag.Op = "Edit";
            return View("CreateEdit",document);
        }

        [HttpPost]
        [CustomAuthorize(Privileges = UserPrivileges.EditDocument)]
        public ActionResult Edit(int id, FormCollection formvalues, HttpPostedFileBase attachment)
        {

            Document document = documentRepository.GetDocument(id);
            if (document == null)
            {
                return View("ShowMessage", "Document does not exists");
            }
            if (TryUpdateModel(document, formvalues))
            {
                string path = documentRepository.uploadDocument(attachment);
                if (path != null)
                {
                    document.Path = path;
                }
                document.UpdateDate = DateTime.Now;
                documentRepository.Save();
                return RedirectToAction("List");
            }
            ViewBag.Op = "Edit";
            return View("CreateEdit",document);
        }

        [CustomAuthorize(Privileges = UserPrivileges.DeleteDocument)]
        public ActionResult Delete(int id)
        {
            Document document = documentRepository.GetDocument(id);
            documentRepository.DeleteDocument(document);
            documentRepository.Save();
            return RedirectToAction("List");
        }

    }
}
